package com.top.utils;

/**
 * @Author:
 * @DateTime: 2023/12/9 21:50
 * @Description: TODO
 */
public class FormatUtil {

    public static String format(Integer length,Object... objs){
        StringBuilder str = new StringBuilder();
        for (Object o : objs) {
            if (o==null){
                o = "";
            }
            String format = format(length, o.toString());
            str.append(format);
        }
        return str.toString();
    }

    /**
     * 格式化字符串
     * @param targetLength 目标长度
     * @param originalStr 原始字符串
     */
    public static String format(Integer targetLength,String originalStr){
        if (originalStr.length() < targetLength) {
            int spacesToAdd = (targetLength - originalStr.length()) / 2; // 计算需要添加的空格数量

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < spacesToAdd; i++) {
                sb.append(" "); // 向StringBuilder对象中添加空格
            }
            return sb + originalStr + sb;
        } else {
            return originalStr; // 如果原始字符串已经足够长了，则直接输出原始字符串
        }
    }
}
